package gui;

import java.awt.BorderLayout;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import java.awt.event.MouseMotionAdapter;

public class LoginFrame extends JFrame {

	private Container con = null;
	private JPanel contentPane;
	private boolean mMoveStart = false;
	private Point mMouseClickPoint = null;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginFrame frame = new LoginFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginFrame() {
		getContentPane().addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent mouseEvent) {
				if(mMoveStart){
					moveWindowTo(mouseEvent.getPoint());
					System.out.println("hej");
				}
			}
		});
		
		getContentPane().addMouseListener(
				new MouseAdapter() {
					public void mousePressed(MouseEvent mouseEvent) {
					mMouseClickPoint = mouseEvent.getPoint();
					mMoveStart = true;
					}
					
					public void mouseReleased(MouseEvent mouseEvent){
						mMoveStart = false;
					}
				});
		
		con = getContentPane();
		con.setLayout(null);
		final ImageIcon img = new ImageIcon(LoginFrame.class.getResource("/pics/backgroundlogin.png"));
		setSize(img.getIconWidth(), img.getIconHeight());
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		int X = (screen.width / 2) - ( 542/ 2); // Center horizontally.
		int Y = (screen.height / 2) - (464 / 2); // Center vertically.
		setBounds(X,Y , 441,311);
		setUndecorated(true);
		contentPane = new JPanel(){
			public void paintComponent(Graphics g){
				Image imh = img.getImage();
				Dimension size = new Dimension(imh.getWidth(null), imh.getHeight(null));
				setPreferredSize(size);
				setMaximumSize(size);
				setMinimumSize(size);
				setSize(size);
				getContentPane().setLayout(null);
				g.drawImage(imh,  0, 0, null);
			}
		};
		
		con.add(contentPane);
		contentPane.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
		
		GridBagLayout layout = new GridBagLayout();
		
		JPanel panelContent = new JPanel(layout);
		
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		
		Component horizontalStrut = Box.createHorizontalStrut(110);
		contentPane.add(horizontalStrut, BorderLayout.EAST);
		
		Component horizontalStrut_1 = Box.createHorizontalStrut(110);
		contentPane.add(horizontalStrut_1, BorderLayout.WEST);
		
		Component verticalStrut_1 = Box.createVerticalStrut(50);
		contentPane.add(verticalStrut_1, BorderLayout.SOUTH);
		
		LoginPanel loginPanel = new LoginPanel();
		contentPane.add(loginPanel, BorderLayout.CENTER);
		
		MenuLogin menuLogin = new MenuLogin();
		contentPane.add(menuLogin, BorderLayout.NORTH);
		
		final JLabel label = new JLabel("");
		label.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseMoved(MouseEvent arg0) {
				label.setIcon(new ImageIcon(LoginFrame.class.getResource("/pics/application_exit2.png")));
			}
		});
		label.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				LoginFrame.this.dispose();
			}
			@Override
			public void mouseExited(MouseEvent arg0) {
				label.setIcon(new ImageIcon(LoginFrame.class.getResource("/pics/application_exit.png")));
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				label.setIcon(new ImageIcon(LoginFrame.class.getResource("/pics/application_exit_pressed.png")));
			}
		});
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		label.setIcon(new ImageIcon(LoginFrame.class.getResource("/pics/application_exit.png")));
		
		
		
		menuLogin.add(label);

		
	}
	
	private void moveWindowTo(Point point){
		int musDiffX = point.x - mMouseClickPoint.x;
		int musDiffY = point.y - mMouseClickPoint.y;
		
		LoginFrame.this.setLocation(getLocation().x + musDiffX, getLocation().y + musDiffY);
		
	}
	
}
